package com.qms.app.service.impl;

import com.qms.app.bean.Question;
import com.qms.app.bean.Users;
import com.qms.app.dao.QuestionDao;
import com.qms.app.service.QuestionService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class QuestionServiceImpl  implements QuestionService {
    @Autowired
    private QuestionDao questionDao;


    //    此处的方法  不能写在外边 必须配置到方法内  注意session的生命周期
    @Override
    public List<Question> listQuestion(Integer page, Integer rows, String title) {
        Users user=(Users) SecurityUtils.getSubject().getSession().getAttribute("user");
        return questionDao.list((page-1)*rows,rows,title,user.getUname());
    }


    public int countQuestion(String title) {
        Users user=(Users) SecurityUtils.getSubject().getSession().getAttribute("user");
        return questionDao.count(title,user.getUname());
    }

    @Override
    public boolean save(Question question) {
        //  用户id通过session进行获取
        Users user = (Users) SecurityUtils.getSubject().getSession().getAttribute("user");
        System.out.println(user.getUid());
        question.setUid(user.getUid());
        return questionDao.save(question)>0;
    }

    @Override
    public boolean remove(List<Integer> ids) {
        Users user = (Users) SecurityUtils.getSubject().getSession().getAttribute("user");
        System.out.println(user.getUid());
        return questionDao.remove(ids, user.getUid())==1;
    }

    @Override
    public boolean update(Question question) {
        Users user =(Users) SecurityUtils.getSubject().getSession().getAttribute("user");
        return questionDao.update(question,user.getUid())==1;
    }

}
